package teach.aistar.rbm;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

/**
 * 本类用来演示: 消费者
 *
 * @author: success
 * @date: 2021/5/12 1:51 下午
 */
@Component
public class RbmService {

    /**
     * 如果放入的数据是字符串 - 该方法的第一个参数代表的就是你要消费的数据 - String
     *
     * @RabbitListener(queues = "x001")
     * 消费者订阅了队列名为x001的消息.
     *
     * @param
     */
//    @RabbitListener(queues = "x001")
//    @RabbitHandler
//    public void direct(String msg, @Headers Map<String,Object> headers, Channel channel){
//        System.out.println("msg:"+msg);
//
//        //手动确认删除操作 - 手动ack
//        long ack = (long) headers.get(AmqpHeaders.DELIVERY_TAG);
//        try {
//            channel.basicAck(ack,false);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }

    //直接模式
    @RabbitListener(queues = "x001")
    @RabbitHandler
    public void direct(Map<String,Integer> datas, @Headers Map<String,Object> headers, Channel channel){
        System.out.println("msg:"+datas);

//        手动确认删除操作 - 手动ack
        long ack = (long) headers.get(AmqpHeaders.DELIVERY_TAG);
        try {
            channel.basicAck(ack,false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //==============分裂模式=============
    @RabbitListener(queues = "y01")
    @RabbitHandler
    public void fanout01(Map<String,Integer> datas, @Headers Map<String,Object> headers, Channel channel){
        System.out.println("msg-y01:"+datas);

        //手动确认删除操作 - 手动ack
        long ack = (long) headers.get(AmqpHeaders.DELIVERY_TAG);
        try {
            channel.basicAck(ack,false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RabbitListener(queues = "y02")
    @RabbitHandler
    public void fanout02(Map<String,Integer> datas, @Headers Map<String,Object> headers, Channel channel){
        System.out.println("msg-y02:"+datas);

        //手动确认删除操作 - 手动ack
        long ack = (long) headers.get(AmqpHeaders.DELIVERY_TAG);
        try {
            channel.basicAck(ack,false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = "y03"),
            exchange =@Exchange(value = "j13_fanout",type = "fanout")))
    @RabbitHandler
    public void fanout03(Map<String,Integer> datas, @Headers Map<String,Object> headers, Channel channel){
        System.out.println("msg-y03:"+datas);

        //手动确认删除操作 - 手动ack
        long ack = (long) headers.get(AmqpHeaders.DELIVERY_TAG);
        try {
            channel.basicAck(ack,false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //=====================
    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = "y03"),
            exchange =@Exchange(value = "j13_topic",type = "topic")))
    @RabbitHandler
    public void topic(Map<String,Integer> datas, @Headers Map<String,Object> headers, Channel channel){
        System.out.println("msg-y03:"+datas);

        //手动确认删除操作 - 手动ack
        long ack = (long) headers.get(AmqpHeaders.DELIVERY_TAG);
        try {
            channel.basicAck(ack,false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
